package niconama04.model.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import niconama04.model.RandomLoader;
import niconama04.shared.data.Category;
import niconama04.shared.data.CategoryOriginal;
import niconama04.shared.data.Constants;
import niconama04.shared.data.Constants.Pos;
import niconama04.shared.data.Grammer;
import niconama04.shared.data.GrammerWord;
import niconama04.shared.data.Original;
import niconama04.shared.data.Sentence;
import niconama04.shared.data.Word;


class RandomLoaderImpl implements RandomLoader {

	private final Dao dao;

	private static final Random random = new Random();

	RandomLoaderImpl(Dao x_dao) {
		dao = x_dao;
	}

	@Override
	public Sentence createSentence(Category x_category) {
		Original original = loadOriginal(x_category);
		if (original == null) {
			return null;
		}
		return createSentence(original);
	}

	@Override
	public Sentence createSentence(Original x_original) {
		Word word = loadWord(x_original);
		if (word == null) {
			return null;
		}
		GrammerWord grammerWord = loadGrammerWord(word);
		if (grammerWord == null) {
			return null;
		}
		Grammer grammer = dao.getGrammer(grammerWord.getGrammerKey());
		if (grammer == null) {
			return null;
		}
		List<Pos> poss = Constants.getPoss(grammer.getConfiguration());
		List<Word> words = new ArrayList<Word>();
		for (int i = 0 ; i < poss.size(); i++) {
			if (i == grammerWord.getIndex()) {
				words.add(word);
			} else {
				Word w = loadWord(grammer, i);
				if (w == null) {
					return null;
				}
				words.add(w);
			}
		}
		Sentence result = new Sentence();
		result.setGrammer(grammer);
		result.setWords(words);
		{
			StringBuilder sb = new StringBuilder();
			for (Word w : words) {
				sb.append(w.getSurface());
			}
			result.setSentence(sb.toString());
		}
		return result;
	}

	private Word loadWord(Grammer x_grammer, int x_index) {
		List<GrammerWord> ws = dao.getGrammerWords(x_grammer.getKey(), null, x_index);
		if (ws.isEmpty()) {
			return null;
		}
		int index = random.nextInt(ws.size());
		GrammerWord gw = ws.get(index);
		return dao.getWord(gw.getWordKey());
	}

	private GrammerWord loadGrammerWord(Word x_word) {
		List<GrammerWord> gws = dao.getGrammerWords(null, x_word.getKey());
		if (gws.isEmpty()) {
			return null;
		}
		int index = random.nextInt(gws.size());
		return gws.get(index);
	}

	private Word loadWord(Original x_original) {
		// TODO 実装を追加する
		List<Word> ws = dao.getWords(x_original.getKey());
		if (ws.isEmpty()) {
			return null;
		}
		int index = random.nextInt(ws.size());
		return ws.get(index);
	}

	private Original loadOriginal(Category x_category) {
		List<CategoryOriginal> cos = dao.getCategoryOriginals(x_category.getKey(), null);
		if (cos.isEmpty()) {
			return null;
		}
		int index = random.nextInt(cos.size());
		CategoryOriginal co = cos.get(index);
		return dao.getOriginal(co.getOriginalKey());
	}
}
